﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.Data;
using Agile.Engine.Utils;
using System.Data;

namespace Fis.Web.Rules.Book
{
    public class UserRule : DynamicRule
    {

        string TableName = "TUSER";

        void AddNew(System.Data.DataTable data)
        {
            var valid = Model.Count(TableName, "FUSERNAME", string.Format("FUSERNAME='{0}'", data.Rows[0]["FUSERNAME"]));
            if (valid > 0)
            {
                Status = -1;
                Message = "名称已存在";
                return;
            }

            data.Rows[0]["SID"] = Guid.NewGuid().ToString();
            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            data.Rows[0]["SBOOKID"] = BookID;
            data.AcceptChanges();
            data.Rows[0].SetAdded();
            data.TableName = TableName;
            Model.UpdateTable(data);
        }

        void Modify(System.Data.DataTable data)
        {
            var valid = Model.Count(TableName, "FUSERNAME", string.Format("FUSERNAME='{0}' AND SID!='{1}'", data.Rows[0]["FUSERNAME"], data.Rows[0]["SID"]));
            if (valid > 0)
            {
                Status = -1;
                Message = "名称已存在";
                return;
            }

            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            //data.AcceptChanges();
            //data.Rows[0].SetModified();
            data.TableName = TableName;
            Model.UpdateTable(TableName, "SID", data.Rows[0]);
            UpdateAdminPwd(data.Rows[0]["FPASSWORD"].ToString());
        }

        /// <summary>
        /// 冻结
        /// </summary>
        /// <param name="sid"></param>
        void Delete(string sid)
        {
            //var count = Model.ExecuteScalar(0, "SELECT COUNT(*) FROM TACCOUNT WHERE FBZ='" + sid + "'");
            //if (count > 0)
            //{
            //    Status = -1;
            //    Message = "币种已被使用，不能删除";
            //}

            Model.ExecuteNonQuery(
                string.Format("UPDATE {0} SET FSTATUS=-1 , SINDEXDATE = GETDATE() WHERE SID='{1}'", TableName, sid));
            //CommonRule common = new CommonRule();
            //common.UpdateDeleteInfo(Model.DataContext, TableName, sid, BookID);
        }

        DataTable NewPassword()
        {
            var data = Model.QueryTable(TableName, "SID='" + UserSid + "'");
            data.Rows[0]["FPASSWORD"] = Paramater["NewPwd"];
            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            Model.UpdateTable(data, TableName, "SID='" + UserSid + "'");
            UpdateAdminPwd(data.Rows[0]["FPASSWORD"].ToString());
            return data;
        }

        void UpdateAdminPwd(string newpwd)
        {
            if (IsAdmin)
            {
                CommonRule rule = new CommonRule();
                rule.ResetServicePassword(BookID, newpwd, BookYear);
            }
        }

        public override void ExecuteNonTrans()
        {
            var action = Paramater["Action"].ToString();
            System.Data.DataTable data = null;
            Status = 1;

            switch (action)
            {
                case "AddNew":
                    data = ZipUtils.UnZipDataTable(Paramater["viewData"].ToString());
                    AddNew(data);
                    break;
                case "Modify":
                    data = ZipUtils.UnZipDataTable(Paramater["viewData"].ToString());
                    Modify(data);
                    break;
                case "Delete":
                    Delete(Paramater["SID"].ToString());
                    break;
                case "password":
                    data = NewPassword();
                    break;
            }

            if (data != null)
                Result["viewData"] = ZipUtils.ZipDataTable(data);

        }
    }
}
